using System;
using System.Collections.Generic;
using System.Text;
using VisualizationPackage.FileLoader;
using System.Collections.ObjectModel;
using Visualization;

namespace VisualizationPackage.Simple_Coloring
{
    public class SimpleColoring
    {
        private ColorMap _map;
        public ColorMap colorsMap 
        {
            get { return _map; }
            set { _map = value; }
        }
        private List<double> _dist;
        public List<double> Distibution 
        {
            get { return _dist; }
            set { _dist = value; }
        }
        private string _variable;
        public string DistributionVariable 
        {
            get { return _variable; }
            set { _variable = value; }
        }

        public SimpleColoring(ColorMap mp)
        {
            _map = mp;
            _dist = new List<double>();
            foreach (ColorMapNode clr in mp.Map)
                _dist.Add(clr.Value);
            _dist.Sort();
        }

		public SimpleColoring(ColorMap mp, string var) : this(mp)
		{
			_variable = var;
		}

        public ReadOnlyCollection<Line> EdgeColoring(Visualization.Zone data)
        {
            //int ElementVertices = (data.Faces[0].Vertices.Length == 4) ?
                //4 : (data.Faces[0].Vertices.Length == 3) ? 3 : 0;
            List<Line> lineslist = new List<Line>();
			for (int i = 0; i < data.EdgeCount; i++)
			{
                //if (i % ElementVertices == 0)
                //    continue;
				Line ln = new Line(new VisualizationPackage.FileLoader.Vertex(data.Vertices[data.Edges[i].Start]), 
                    new VisualizationPackage.FileLoader.Vertex(data.Vertices[data.Edges[i].End]));//data.Vertics[data.Topology[i - 1]-1], data.Vertics[data.Topology[i]-1]);
				ln.DrawingColor = _map[Distribute(avg(ln.Vertex1, ln.Vertex2))];
				lineslist.Add(ln);
			}
            return new ReadOnlyCollection<Line>(lineslist);
        }

        public ReadOnlyCollection<Polygon> SurfaceColoring(Visualization.Zone data)
        {
            int ElementVertices = data.Faces[0].Vertices.Length;
            List<Polygon> polslist = new List<Polygon>();
			for (int i = 0; i < data.FaceCount; i++)
			{
				Polygon pol = new Polygon();
				for (int j = 0; j < data.Faces[i].Vertices.Length; j++)
					pol.Vertics.Add(new VisualizationPackage.FileLoader.Vertex(data.Vertices[data.Faces[i].Vertices[j]]));// data.Vertics[ data.Topology[i]-1 ]  );
				pol.DrawingColor = _map[Distribute(midPoint(pol, ElementVertices))];
				polslist.Add(pol);
			}
            return new ReadOnlyCollection<Polygon>(polslist);
        }

        private double midPoint(Polygon pol, int nVerts)
        {
           // VisualizationPackage.FileLoader.Vertex Last;// = new VisualizationPackage.FileLoader.Vertex();
           // Last = pol.Vertics[pol.Vertics.Count - 1];
            
            double count = 0;
            for (int i = 0; i < nVerts; i++)
                count += pol.Vertics[i][_variable];
        //    count += Last[_variable];
            return count / nVerts;
        }

        private double avg(VisualizationPackage.FileLoader.Vertex vertex, VisualizationPackage.FileLoader.Vertex vertex_2)
        {
            return (vertex[_variable] + vertex_2[_variable]) / 2.0;
        }

        private double Distribute(double value)
        {
            for (int i = 1; i < _dist.Count; i++)
            {
                if (_dist[i] > value)
                    return _dist[i - 1];
            }
            return _dist[ _dist.Count -1 ];
        }
    }
}
